<?php
/**
 * 产品列表
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Category;
use Common\Library\Page;
class ProductController extends CommonController{
	private $Model;
	private $msg=array();
	function _initialize(){
		parent::_initialize();
		$this->Model=D('Product');
	}
	public function main(){
		$this->redirect('index');
	}
	/**
	 * 产品列表
	 */
	public function index(){
		if(!$this->checkPermission('Products')) $this->jump();
		$data=array();
		if(I('get.title')) $data['title']=I('get.title');
		if(I('get.status')) $data['status']=I('get.status');
		if(I('get.category_id')) $data['category_id']=I('get.category_id');
		$filter_data=I('get.');
		if(isset($filter_data['category_id'])){
			$category=D('Category')->getCategory($filter_data['category_id']);
		}
		if(isset($category) && $category){
		$filter_data['category_title']=$category['category_description'][get_default_lng_id()]['title'];
		}
		$count=$this->Model->getCount($data);
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$data['start']=$Page->firstRow;
		$data['limit']=$Page->listRows;
		$list=$this->Model->getProducts($data);
		foreach ($list as $k => $v) {
			$list[$k]['thumb']=image_resize($v['image'],100,100);
		}

		$show=$Page->show();
		$url=array(
			'title'		=>	I('get.title','','trim'),
			'category_id'=>	I('get.category_id','','trim'),
			'status'	=>	I('get.status','','trim'),
			'p'			=>	I('get.p','','trim')
			);

		$this->assign('filter_data',$filter_data);
		$this->assign('list',$list);
		$this->assign('url',$url);
		$this->assign('page',$show);
		$this->display();

	}

	public function oper($product_id=0){
		if(!$product_id && !$this->checkPermission('AddProduct')) $this->jump();	
		$languages=get_langs();
		$product=$this->Model->getProduct($product_id);
		if($product_id && empty($product)) $this->jump(L('error_404'));
		if(IS_POST){
			$product=I('post.');
			if($this->vilidateForm()){
				$this->Model->operProduct($product,$product_id);
				session('success',$product_id?L('success_edit'):L('success_add'));
				$url=array(
					'title'		=>	I('get.title','','trim'),
					'category_id'=>	I('get.category_id','','trim'),
					'status'	=>	I('get.status','','trim'),
					'p'			=>	I('get.p','','trim')
					);
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');				
			}
		}
		$product['placeholder']=image_resize();
		if(isset($product['image']) && is_file(C('IMAGE_DIR').$product['image'])){
			$product['thumb']=image_resize($product['image'],100,100);
		}else{
			$product['thumb']=$product['placeholder'];		
		}		
		$product_image_temp=array();
		foreach($product['product_image'] as $product_image){
			if(is_file(C('IMAGE_DIR').$product_image['image'])){
				$image 	=	$product_image['image'];
				$thumb  =	image_resize($product_image['image'],100,100);
			}else{
				$image  =	'';
				$thumb  =	image_resize();
			}
			$product_image_temp[]=array(
				'image'	=>	$image,
				'thumb' =>	$thumb,
				'sort_order'	=>	$product_image['sort_order'],
				'text'	=> $product_image['text']
				);
		}		
		$product['product_image']=$product_image_temp;
		$language_id=get_default_lng_id();
		foreach ($product['product_related'] as $k => $v) {
			$product_info=$this->Model->getProduct($v);
			$product['product_related'][$k]=array(
				'product_id'	=>	$v,
				'title'			=>	$product_info['product_description'][$language_id]['title']
				);
		}
		if(isset($product['product_download']) || !empty($product['product_download'])){
			$DownloadModel=D('Download');
			foreach ($product['product_download'] as $k => $v){
				$download_info=$DownloadModel->getDownload($v);
				$product['product_download'][$k]=array(
					'download_id'	=>	$v,
					'name'			=>	$download_info['download_description'][$language_id]['name']
					);
			}
		}
		$this->assign('languages',$languages);	
		$categories=D('Category')->getCategories($data_filter);
		$categories=Category::unlimitedForLevel($categories,'',0,0,'category_id','parent_id');
		$this->assign('categories',$categories);
		$this->assign('msg',$this->msg);
		$this->assign('product',$product);
		$this->display();
	}

	/**
	 * 数据验证
	 */
	private function vilidateForm(){
		$product_id=I('get.product_id',0);
		if($product_id && !$this->checkPermission('EditProduct')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		foreach($data['product_description'] as $language_id => $value){
			if(mb_strlen($value['title'])<2 || mb_strlen($value['title'])>255){
				$this->msg['title'][$language_id]=L('error_title');
			}
			if(mb_strlen($value['meta_title'])>255){
				$this->msg['meta_title'][$language_id]=L('error_meta_title');
			}
			if(mb_strlen($value['meta_description'])>255){
				$this->msg['meta_description'][$language_id]=L('error_meta_description');
			}
			if(mb_strlen($value['meta_keyword'])>255){
				$this->msg['meta_keyword'][$language_id]=L('error_meta_keyword');
			}

		}
		return !$this->msg;
	}

	/**
	 * 删除
	 */
	public function del(){
		if($this->checkPermission('DeleteProduct')){
			if($this->vilidateDelete()){
				foreach (I('selected') as $product_id) {
					$count += $this->Model->del($product_id);
				}
				if($count){
					session('success',L('success_delete',array('count'=>$count)));
				}
			}else{
				session('danger',L('error_delete'));
			}
		}else{
			session('danger',L('error_not_permission'));
		}
		$this->redirect('index');
	}

	/**
	 * 删除验证
	 */
	private function vilidateDelete(){
		return true;
	}

	/**
	 * 自动完成
	 */
	public function autocomplete(){
		$json=array();
		$filter_data=array(
			'start'			=>	0,
			'limit'			=>	8,			
			);
		if(I('get.filter_title')){
			$filter_data['title']=I('get.filter_title');
		}
		if(I('get.category_id')){
			$filter_data['category_id']=I('get.category_id');
		}
		if(I('get.status')){
			$filter_data['status']=I('get.status');
		}

		$products=D('Product')->getProducts($filter_data);

		foreach ($products as $v) {
			$json[]=array(
				'product_id'=>	$v['product_id'],
				'category_id'=>	$v['category_id'],
				'title'		=>	$v['title']
				);
		}
		$this->ajaxReturn($json,'JSON');
	}
}